home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
pascal
/
spoc88.zip
/
DBDEMO.ZIP
/
DEMO.C
next >
Wrap
Text File
|
1988-07-01
|
4KB
|
99 lines
/*
* Turbo C 2.0 program for use in a sample Turbo Debugger
* debugging session. The bug: the Text array in the
* TextBlock structure does not include space for the
* terminating zero byte. The solution: dimension the
* Text array to (BUFFER_LENGTH + 1) characters in length.
*
* By Michael Abrash 6/18/88
*/
#include <stdio.h>
#include <alloc.h>
/* Number of characters buffered per text block. */
#define BUFFER_LENGTH 20
/* Structure we'll use to store text in. These structures
are combined into a singly-linked list, with one
structure per allocated memory buffer. */
struct TextBlock {
char Text[BUFFER_LENGTH]; /* text buffer */
struct TextBlock *NextTextBlock; /* pointer to next
text block */
};
main()
{
int c; /* temporary storage for a character */
int Done = 0; /* set to 1 when all text is buffered */
int TextCount; /* location in the current text buffer */
struct TextBlock *FirstTextBlock;
/* Points to the text block that
starts the linked chain. */
struct TextBlock *CurrentTextBlock;
/* points to the current text block */
struct TextBlock *NewTextBlock;
/* points to the next text block */
/* Get the initial text block */
if ( !(FirstTextBlock = CurrentTextBlock =
malloc(sizeof(struct TextBlock))) ) {
/* We couldn't get any memory */
printf("Out of memory\n");
exit(1);
}
/* Buffer the text the user types, allocating memory as
it's needed */
TextCount = 0;
while ( !Done ) {
/* Get the next character */
c = getchar();
if ( c == EOF ) {
/* It's the end of the file, so we're done */
/* Put a zero at the end of the current buffer,
making it a string */
CurrentTextBlock->Text[TextCount] = 0;
/* Mark that this is the last text block in the
linked list */
CurrentTextBlock->NextTextBlock = 0;
/* We've gotten all the text */
Done = 1;
} else {
/* Buffer the character */
CurrentTextBlock->Text[TextCount++] = toupper(c);
if ( TextCount >= BUFFER_LENGTH ) {
/* This buffer's full, so allocate another
text block */
if ( !(NewTextBlock =
CurrentTextBlock->NextTextBlock =
malloc(sizeof(struct TextBlock))) ) {
/* We couldn't get any more memory */
printf("Out of memory\n");
exit(1);
}
/* Put a zero at the end of the current buffer,
making it a string */
CurrentTextBlock->Text[TextCount] = 0;
/* Start buffering at the beginning of this
text block's text buffer */
TextCount = 0;
/* Make the newly allocated text block the
current text block */
CurrentTextBlock = NewTextBlock;
}
}
}
/* Print out the uppercase result, starting with the
text stored in the first text block and continuing
until the last text block (the text block with a
null link) has been displayed */
CurrentTextBlock = FirstTextBlock;
do {
printf("%s", CurrentTextBlock->Text);
CurrentTextBlock = CurrentTextBlock->NextTextBlock;
} while ( CurrentTextBlock );
}